import CodoBase from "../../../lib/CodoBase";
import * as qs from "querystring";
import { Wechat } from "../wechat";
import CodoError from "../../../lib/CodoError";

export default class CodoWechatMPLogin extends CodoBase {
	/** 小程序登录，前端拿code来换session */
	async jscode2session(appid: string, secret: string, jscode: string) {
		const params = qs.stringify({ appid, secret, js_code: jscode, grant_type: "authorization_code" });
		const url = `https://api.weixin.qq.com/sns/jscode2session?${params}`;
		const res = await this.app.curl<Wechat.mp.response.jscode2session>(url, {
			method: "GET",
			contentType: "json",
			dataType: "json",
		});
		if (res.data.errcode) {
			throw new CodoError(`小程序登录jscode2session失败，错误码 %s，错误信息 %s`, res.data.errcode, res.data.errmsg);
		}
		return res.data;
	}

	async checkSessionKey() {}

	async resetUserSessionKey() {}
}
